/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.archwood.frc2607.utility;

import com.sun.squawk.microedition.io.FileConnection;
import java.io.PrintStream;
import javax.microedition.io.Connector;

/**
 *
 * @author Ron
 */
public class ConsoleLogger {
    private PrintStream consoleFile = null;
    private static ConsoleLogger instance = new ConsoleLogger();

    private ConsoleLogger() {
        String fileName = "file:///ConsoleLog" + String.valueOf(System.currentTimeMillis()) +
                          ".csv";
        try {
            FileConnection fc = (FileConnection)Connector.open(fileName,
                                                Connector.WRITE);
            fc.create();
            consoleFile = new PrintStream(fc.openOutputStream());
            consoleFile.println("Time (ms), Message");
        }
        catch (Exception e) {
            consoleFile = null;
            System.out.println("ERROR creating console log file " + fileName);
        }
    }

    public static ConsoleLogger getInstance() {
        return instance;
    }
    
    public void log(String line) {
        String lineToConsole = Long.toString(System.currentTimeMillis()) + "," +
                               line;
        if (consoleFile != null)
            consoleFile.println(lineToConsole);
        System.out.println(lineToConsole);
    }

}
